using System.Data;
using Front._1124;
using MySql.Data.MySqlClient;

namespace Front._1124;

public partial class BookMain : Form
{
    public BookMain()
    {
        InitializeComponent();
    }
    private void BookMain_Load(object sender, EventArgs e)
    {
        //初始化操作
        //查询所有的图书
        GetAllBooks();
        
        //查询所有的图书分类
        GetAllCategories();
    }

    private void GetAllCategories()
    {
        using var connection = new MySqlConnection("server=localhost;port=3306;database=book;user=root;password=root;charset=utf8mb4;AllowPublicKeyRetrieval=True;");
        connection.Open();
        String sql = "select DISTINCT book_category from book_info";

        MySqlDataAdapter adapter = new MySqlDataAdapter(sql,connection);
        DataSet DataSet = new DataSet();
        adapter.Fill(DataSet,"book_categories");
        
        DataTable?dataTable = DataSet.Tables["book_categories"];
        
        DataRow row = dataTable.NewRow();
        row[0] = "全部";
        dataTable.Rows.InsertAt(row,0);
        
        this.comboBox2.DataSource = dataTable;
        this.comboBox2.DisplayMember = dataTable.Columns[0].ColumnName;
    }

    private void GetAllBooks()
    {
        using var connection = new MySqlConnection("server=localhost;port=3306;database=book;user=root;password=root;charset=utf8mb4;AllowPublicKeyRetrieval=True;");
        connection.Open();
        
        String sql = "SELECT book_id,isbn,book_name,author FROM  book_info";
        
        MySqlDataAdapter adapter = new MySqlDataAdapter(sql, connection);

        DataSet dataSet = new DataSet();
        
        adapter.Fill(dataSet, "book_info");
        
        this.dataGridView1.DataSource = dataSet.Tables["book_info"];
    }

    private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        DialogResult result = MessageBox.Show("确定要删除此条数据吗", "警告", MessageBoxButtons.OKCancel);

        if (result == DialogResult.OK)
        {
            int bookId = (int)this.dataGridView1.SelectedRows[0].Cells[0].Value;
            using var connection =
                new MySqlConnection(
                    "server=localhost;port=3306;database=book;user=root;password=root;charset=utf8mb4;AllowPublicKeyRetrieval=True;");
            connection.Open();
            string sql = "delete from book_info where book_id=@book_id";

            using var command = new MySqlCommand(sql, connection);
            command.Parameters.AddWithValue("bookId", bookId);

            int count = command.ExecuteNonQuery();
            if (count > 0)
            {
                MessageBox.Show("删除成功");
                GetAllBooks();
            }
            else
            {
                MessageBox.Show("删除成功");
            }
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        ADDBookForm addBookForm = new ADDBookForm();
        DialogResult result = addBookForm.ShowDialog();
        if (result == DialogResult.OK)
        {
            GetAllBooks();
        }
    }
}

internal class ADDBookForm
{
    public DialogResult ShowDialog()
    {
        throw new NotImplementedException();
    }
}